package pkg

import (
	"encoding/json"
	"log"
	"post-hub/global"
)

type websocket struct {
}
type Message struct {
	UserId  int    `json:"userId"`
	DistId  int    `json:"distId"`
	Content string `json:"content"`
}

func NewWebsocket() *websocket {

	return &websocket{}
}
func ReadMessage(node *global.Node) {

	for {

		_, message, err := node.Conn.ReadMessage()
		if err != nil {
			log.Panicln(err)
			return
		}
		var msg Message
		err = json.Unmarshal(message, &msg)
		if err != nil {
			node.Data <- []byte("消息类型错误")
			return
		}
		n, ok := global.ClientMap[int64(msg.UserId)]
		if !ok {
			n.Data <- message
		}
	}
}
